package com.betty.cscams.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.betty.cscams.common.page.MybatisPageHelper;
import com.betty.cscams.common.page.PageRequest;
import com.betty.cscams.common.page.PageResult;
import com.betty.cscams.dao.BusUserSubjectMapper;
import com.betty.cscams.dao.SysUserMapper;
import com.betty.cscams.entity.BusUserSubject;
import com.betty.cscams.entity.SysDept;
import com.betty.cscams.entity.SysUser;
import com.betty.cscams.util.SecurityUtils;
import org.apache.commons.collections4.ListUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import com.betty.cscams.entity.BusSubject;
import com.betty.cscams.dao.BusSubjectMapper;
import com.betty.cscams.service.BusSubjectService;
import org.springframework.transaction.annotation.Transactional;

/**
 * ---------------------------
 *  (BusSubjectServiceImpl)         
 * ---------------------------
 * 时间：  2022-04-03 12:19:27
 * ---------------------------
 */
@Service
public class BusSubjectServiceImpl implements BusSubjectService {

	@Autowired
	private BusSubjectMapper busSubjectMapper;
	@Autowired
	private BusUserSubjectMapper busUserSubjectMapper;
	@Autowired
	private SysUserMapper sysUserMapper;

	@Override
	@Transactional(rollbackFor = Exception.class)
	public int save(BusSubject record) {
		record.setLastUpdateBy(SecurityUtils.getUsername());
		record.setLastUpdateTime(new Date());
		if(record.getId() == null || record.getId() == 0) {
			record.setCreateBy(SecurityUtils.getUsername());
			record.setCreateTime(new Date());
			return busSubjectMapper.add(record);
		}
		List<BusUserSubject> list = record.getUserSubjectList();
		busUserSubjectMapper.deleteBySubjectId(record.getId());
		if(list != null && !list.isEmpty()){
			for (BusUserSubject object:list) {
				busUserSubjectMapper.add(object);
			}
		}
		return busSubjectMapper.update(record);
	}

	@Override
	public int delete(BusSubject record) {
		return busSubjectMapper.delete(record.getId());
	}

	@Override
	public int delete(List<BusSubject> records) {
		for(BusSubject record:records) {
			delete(record);
		}
		return 1;
	}

	@Override
	public BusSubject findById(Long id) {
		return busSubjectMapper.findById(id);
	}

	@Override
	public PageResult findPage(PageRequest pageRequest) {
		Object  termId = pageRequest.getParam("termId").toString();
		Object  subjectName = pageRequest.getParam("subjectName").toString();
		Object  subjectType = pageRequest.getParam("subjectType").toString();
		return MybatisPageHelper.findPage(pageRequest, busSubjectMapper,"findPage",termId,subjectName,subjectType);
	}
	@Override
	public List<SysDept> findUsersBySubject(Long subjectId) {
		return busSubjectMapper.findUsersBySubject(subjectId);
	}

	@Override
	public List<BusSubject> findSubjectOptions(Map<String,Object> params) {
		SysUser sysUser = sysUserMapper.findByName(SecurityUtils.getUsername());
		if(ObjectUtils.isNotEmpty(params.get("studentNum"))){
			params.put("studentId",sysUserMapper.findByName(params.get("studentNum").toString()).getId());
		}
		params.put("userId",sysUser.getId());
		params.put("type",sysUser.getType());
		return busSubjectMapper.findSubjectOptions(params);
	}

	@Override
	public List<BusSubject> findOptions(Map<String, Object> params) {
		return busSubjectMapper.findOptions(params);
	}
}
